import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-06-01
 * Time: 23:28
 */


class User {
    public String username;
    private String password;
    public double score;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(String username, String password, double score) {
        this.username = username;
        this.password = password;
        this.score = score;
    }
    public User() {

    }
}

@WebServlet("/show_json")
public class showJSON extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();
    //解析json的核心类就是，ObjectMapper


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> map = new HashMap<String, String>() {
            {
                put("abc", "1");
                put("def", "2");
                put("ghi", "0");
            }
        };
        String ret = objectMapper.writeValueAsString(map);
        resp.getWriter().write(ret);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");

//        User user = objectMapper.readValue(req.getInputStream(), User.class);
//        System.out.println(user.username + " " + user.getPassword() + " " + user.score);
        resp.setContentType("application/json; charset=utf8");
        Map<String, String> map = objectMapper.readValue(req.getInputStream(), HashMap.class);
        System.out.println(map.get("username") + " " +
                map.get("password") + " " + map.get("score"));



    }
}
